package ext.acae2.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import wt.log4j.LogR;
import wt.util.WTProperties;

public class TCWCMappingDataHelper {
	
	public static final String TYPE_MAPPING_CONFIG="modelTYPE"; //TC数据集的类型的数据
	public static final String TYPE_DOC_MAPPING_CONFIG="DOCTYPE"; //TC和WC的文档之间的映射
	
	private static final Logger LOGGER = LogR
			.getLogger(TCWCMappingDataHelper.class.getName());
	private static String typePathFolder;
	static {
		try {
			String codebasePath = WTProperties.getLocalProperties()
					.getProperty("wt.codebase.location");
			String tempPath = WTProperties.getLocalProperties().getProperty(
					"wt.temp");
			typePathFolder = codebasePath + File.separator + "ext"
					+ File.separator + "acae2" + File.separator + "dms";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param type 那种类型映射
	 * @param WCType  windchill的类型
	 * @return TC对应的类型
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static String  getTCWCMapping(String type,String WCType) throws Exception{
		String TCTypeValue=null;
		String typeFilePath = null;
		if(TYPE_MAPPING_CONFIG.equals(type)){
			typeFilePath = typePathFolder+File.separator + "typemapping"+File.separator+"type.properties";
		}
		Properties props = new Properties();
		props.load(new FileInputStream(typeFilePath));
		Set<Object> keys = props.keySet();
		@SuppressWarnings("rawtypes")
		Iterator it=keys.iterator();
		while(it.hasNext()){
			String key = (String)it.next();
			if(StringUtils.isNotBlank(key.trim())){
				String value = props.getProperty(key).trim();
				if(StringUtils.equalsIgnoreCase(value.trim(), WCType)){
					TCTypeValue =  key.trim();
					break;
				}
			}
		}
		return TCTypeValue;
	}
	
	

}
